import matplotlib.pyplot as plt
import  numpy as np
import math

xhat_prev = 0
x_measure = [0]*4
xhat = [0] * 4

print('请输入：')
for i in range(0,4):
    a = input()
    a = float(a)
    x_measure[i] = a
print('输入的内容是：',x_measure)

T = 1
k = [1, 2,3,4]  #采样次数
K = [0.0] * 4 #滤波器增益
t = [0] * 4

for i in range(0,4):
    t[i] = (k[i] - 1) * T

#0阶最小二乘滤波器的估计值
Compare = (1/len(k))*sum(x_measure)

for i in range(0, len(k)):
    K[i] =1 / k[i]
    xhat[i] =xhat_prev + K[i] *(x_measure[i] - xhat_prev)
    xhat_prev = xhat[i]

plt.xlabel("Time(Sec)")
plt.ylabel("xhat")
plt.plot(t,x_measure,'+')
plt.plot(t,xhat,'--',label = 'zero-order least squares filters')
plt.axhline(y=Compare, c="k", ls="--", lw=2,label = 'zero-order')
plt.legend()
plt.show()
